package org.ytor.core.sysapi.permission.repository;

import org.springframework.stereotype.Repository;
import org.ytor.common.util.Colls;
import org.ytor.core.sqlflow.BaseRepository;
import org.ytor.core.sqlflow.SQLHelper;
import org.ytor.core.sysapi.role.model.SysRolePermission;
import org.ytor.core.sysapi.permission.model.SysPermission;
import org.ytor.core.sysapi.permission.resp.SysPermissionResp;

import java.util.List;

/**
 * created by System on 2025年5月22日 20:45:43
 * <br/>
 * SysPermission表Mapper
 * <br/>
 */
public class SysPermissionRepository extends BaseRepository<SysPermission> {

    public List<SysPermissionResp> queryByPid(String pid) {
        return SQLHelper.select()
                .selectRaw("exists(select 1 from sys_permission where pid = p.id) as hasChildren")
                .from(SysPermission.class)
                .where(w -> w.eq(SysPermission::getPid, pid))
                .list(SysPermissionResp.class);
    }

    public void deleteByRoleAndPermissionIds(String roleId, List<String> deletedPermissionIds) {
        SQLHelper.<SysRolePermission>delete().from(SysRolePermission.class)
                .where(w -> w.eq(SysRolePermission::getRoleId, roleId).in(Colls.isNotEmpty(deletedPermissionIds), SysRolePermission::getPermissionId, deletedPermissionIds));
    }
}